//
//  YaoYiYaoViewController.m
//  wtshoppingcite
//
//  Created by TW on 15/5/20.
//  Copyright (c) 2015年 TW. All rights reserved.
//

#import "YaoYiYaoViewController.h"
#import "LZAudioTool.h"
#import "UIView+LZ.h"
#import "PrizeViewController.h"
#import "PrizeModel.h"

@interface YaoYiYaoViewController ()

@property (nonatomic,weak) UIImageView *up;
@property (nonatomic,strong)PrizeModel *prizeModel;
@property (nonatomic,strong)UIImageView *textImageView;
@end

@implementation YaoYiYaoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitleView:@"摇一摇"];
    
    UIImageView *up = [[UIImageView alloc] init];
    up.image = [UIImage imageNamed:@"bg_yaoyao_left"];
    up.frame = CGRectMake(20, 20,kWidth-40, 180);
    [self.view addSubview:up];
    self.up = up;
    
    if (!IS_4_INCH_SCREEN && kHeight == 480) {
        up.frame = CGRectMake(20, 20,kWidth-40, 140);
    }
    
    UILabel *titleLabel = [self createLabelWithFrame:CGRectMake(0, up.bottom, kWidth, 20) text:@"摇一摇即中奖" font:15.0f textAli:NSTextAlignmentCenter textColor:MAIN_BLUE_COLOR];
    [self.view addSubview:titleLabel];
    
    
    
//    UILabel *subTitleLabel = [self createLabelWithFrame:CGRectMake(5, titleLabel.bottom, kWidth-10, 40) text:@"规则：每天可摇奖5次，首次0积分，之后每次消耗100积分" font:15.0f textAli:NSTextAlignmentLeft textColor:MAIN_BLUE_COLOR];
//    subTitleLabel.numberOfLines = 0;
//    [self.view addSubview:subTitleLabel];

    _textImageView =[[UIImageView alloc]initWithFrame:CGRectMake(10, kHeight -200-64-10, kWidth-20,200)];
    
    if (!IS_4_INCH_SCREEN && kHeight == 480) {
        _textImageView.frame = CGRectMake(10, titleLabel.bottom+35, kWidth-20,kHeight - up.height-titleLabel.height-40-64-20);
    }
    
    _textImageView.image = [UIImage imageNamed:@"catelog.png"];
    [self.view addSubview:_textImageView];
    
    
    NSString *text = @"1、拿起手机在当前页面摇一摇，100%中奖，就是这么任性！\n2、每次摇奖一次消耗50积分。\n3、摇到的奖品发放会有相关客服人员联系，或者拨打400电话：400-8333201。\n4、作弊用户将取消活动资格，并加入活动黑名单。\n5、本活动最终解释权归零点家园所有。\n6、该活动的奖励、抽奖与苹果公司无关;";
    
    UILabel *textSubLabel = [self createLabelWithFrame:CGRectMake(5, 0, kWidth-30, _textImageView.height) text:text font:15.0f textAli:NSTextAlignmentLeft textColor:MAIN_BLUE_COLOR];
    textSubLabel.numberOfLines = 0;
    [_textImageView addSubview:textSubLabel];
    
   
//    subTitleLabel.bottom = _textImageView.top;
     titleLabel.bottom = _textImageView.top;
}


-(BOOL)canBecomeFirstResponder
{
    return YES;
}


-(void)viewDidAppear:(BOOL)animated {
    
    [super viewDidAppear:animated];
    
    [self becomeFirstResponder];
    
}


-(void)viewWillDisappear:(BOOL)animated {
    
    [self resignFirstResponder];
    
    [super viewWillDisappear:animated];
    
}

#pragma mark - 实现相应的响应者方法
/** 开始摇一摇 */
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"motionBegan");
    
    CGFloat duration = 0.4;
    
    [UIView animateWithDuration:duration animations:^{
    
        self.up.image = [UIImage imageNamed:@"bg_yaoyao_right"];
        
    }];
    
    
    [LZAudioTool playMusic:@"shake_sound_male.mp3"];
}

/** 摇一摇结束（需要在这里处理结束后的代码） */
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    // 不是摇一摇运动事件
    if (motion != UIEventSubtypeMotionShake) return;
    
    NSLog(@"motionEnded");

    CGFloat duration = 0.4;
    [UIView animateWithDuration:duration animations:^{
        self.up.image = [UIImage imageNamed:@"bg_yaoyao_left"];
    }];
    
   
    [self getacquirePrize];
    
}

-(void)getacquirePrize{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:[RegisterHelper shareHelper].userId forKey:@"userId"];
    
    [NetWorkService byRequestWithAPIPrefix:kBaseAppUrl
                              andAPISuffix:kPrize
                             andHttpMethod:@"POST"
                             andParameters:params
                           andRequestCache:NO
                            andUseParamDic:YES
                       andPostDataEncoding:MKNKPostDataEncodingTypeJSON
                     andRequestFinishBlock:^(BYRequest *request, BYResponse *response) {
                         
                         NSDictionary *responseDic = request.responseJSON;
                         
                         if(!response.isError){
                             
                             
                             BOOL result= [[responseDic objectForKey:@"result"]boolValue];
                             if (!result) {
                                 DLog(@"数据获取成功 %@",responseDic);
                                 
                                 BOOL result= [[responseDic objectForKey:@"result"]boolValue];
                                 
                                 if (!result) {
                                     _prizeModel = [[PrizeModel alloc]initWithPrizeList:[responseDic objectForKey:@"prizeDTO"]];
                                     
                                     NSString *ad = [responseDic objectForKey:@"ad"];
                                     
                                     PrizeViewController *prizeVC = [[PrizeViewController alloc]init];
                                     prizeVC.prizeModel = _prizeModel;
                                     prizeVC.adv = ad;
                                     prizeVC.hidesBottomBarWhenPushed = YES;
                                     
                                     [self showToastWithMessage:[responseDic objectForKey:@"msg"] showTime:2];
                                     
                                     [self.navigationController pushViewController:prizeVC animated:YES];
                                     

                                 }
                              }
                             
                             else{
                                 
                                 [self showToastWithMessage:[responseDic objectForKey:@"msg"] showTime:2];
                                
                             }
                         }
                         else{
                             
                             DLog(@"获取数据失败  %@",response.errorMessage);
                             
                              NSDictionary *responseDic = request.responseJSON;
                             
                             [self showAlertView:@"提示" message:[responseDic objectForKey:@"msg"]];
                         }
                         
                     }];

}

///** 摇一摇取消（被中断，比如突然来电） */
//- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
//{
//    NSLog(@"motionCancelled");
//}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
